package baidu.reader.common.model
{
    import __AS3__.vec.*;
    import baidu.reader.common.model.VO.*;
    import flash.events.*;

    public class TocDataModel extends EventDispatcher
    {
        private var proxy:TocRemoteDataProxy;
        private var _tocData:Vector.<TocItemVO>;
        private var _tocLoaded:Boolean = false;

        public function TocDataModel(param1:IEventDispatcher = null)
        {
            this._tocData = new Vector.<TocItemVO>;
            super(param1);
            this.proxy = new TocRemoteDataProxy();
            this.proxy.addEventListener(ErrorEvent.ERROR, this.loadErrorHandler);
            this.proxy.addEventListener(Event.COMPLETE, this.loadCompleteHandler);
            return;
        }

        private function loadCompleteHandler(event:Event) : void
        {
            this.parseTocXMLData(this.proxy.xmlData);
            this._tocLoaded = true;
            dispatchEvent(new Event(Event.CHANGE));
            return;
        }

        private function parseTocXMLData(param1:XML) : void
        {
            var _loc_4:TocItemVO = null;
            var _loc_2:* = param1.children().length();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {

                _loc_4 = new TocItemVO();
                _loc_4.docid = String(param1.children()[_loc_3].docid);
                _loc_4.level = int(param1.children()[_loc_3].level);
                _loc_4.page = int(param1.children()[_loc_3].page);
                _loc_4.offset = Number(param1.children()[_loc_3].offset);
                _loc_4.title = String(param1.children()[_loc_3].title);
                this._tocData.push(_loc_4);
                _loc_3++;
            }
            return;
        }

        private function loadErrorHandler(event:ErrorEvent) : void
        {
            dispatchEvent(event);
            return;
        }

        public function getTocData() : void
        {
            if (this._tocLoaded)
            {
                dispatchEvent(new Event(Event.CHANGE));
                return;
            }
            this.proxy.getXMLData();
        }

        public function get tocData() : Vector.<TocItemVO>
        {
            return this._tocData;
        }

        public function get tocLoaded() : Boolean
        {
            return this._tocLoaded;
        }

    }
}
